(function (angular) {
    angular
    .module('todoCtrl',[])
    .controller('todoController', function ($scope, $timeout,$location,DataService) {
        $scope.todoList = DataService.getData();
        // 刷新
        $scope.refulsh = ()=>{
            $scope.todoList = DataService.getData();
        };
        // 添加todo
        $scope.addTodo = function () {
            DataService.insertData($scope.newTodo);
            $scope.newTodo = '';
            $scope.refulsh();
        };
        // 删除
        $scope.removeTodo = function (id) {
            DataService.removeData(id);
            $scope.refulsh();
        };
        //双击修改
        $scope.editTodo = function (todo) {
            todo.editing = true;
            $timeout(function () {
                var editInput = angular.element(document.getElementById(todo.id));
                editInput[0].focus(); // Use [0] to get the native DOM element from Angular's wrapped element
                editInput[0].select(); // Optionally select all text in the input field
            });
        }
        //确认修改
        $scope.doneEdit = function (todo) {
            if (todo.title.trim() === '') {
                alert("不能为空");
                return;
            }
            DataService.updateData(todo);
            $scope.refulsh();
        }
        $scope.allTag = false;
        //反选
        $scope.getStatus = function () {
            for (var index = 0; index < $scope.todoList.length; index++) {
                if (!$scope.todoList[index].completed) {
                    $scope.allTag = false;
                    return;
                }
            }
            $scope.allTag = true;
        }
        //全选
        $scope.checkChange = function () {
            $scope.todoList.forEach(function (todo) {
                todo.completed = $scope.allTag;
            });
        }
        //是否有已完成
        $scope.hasCompleted = function () {
            for (var index = 0; index < $scope.todoList.length; index++) {
                if ($scope.todoList[index].completed) {
                    return true;
                }
            }
            return false;
        }
        //清除已完成
        $scope.clearCompleted = () => {
            DataService.clearCompleted($scope.todoList);
            $scope.refulsh();
        }
        //显示未完成
        $scope.getUnCompleted = function () {
            let list = $scope.todoList.filter(function (todo) {
                return todo.completed == true;
            });
            return list.length;
        }
        //显示已完成
        $scope.showActive = function () {
            let list = $scope.todoList.filter(function (todo) {
                return todo.completed == false;
            });
            return list.length;
        }
        //查看不同状态列表
        $scope.keyword = undefined;
        $scope.changeStatus = function (tag) {
            if(tag==='all'){
                $scope.keyword = undefined;
                return;
            }
            if(tag==='active'){
                $scope.keyword = false;
                return;
            }
            if(tag==='completed'){
                $scope.keyword = true;
                return;
            }
        }
        //监听锚点
        $scope.location=$location;
        $scope.$watch('location.hash()', function (newVal) {
            switch (newVal) {
                case '/active':
                    $scope.keyword = false;
                    break;
                case '/completed':
                    $scope.keyword = true;
                    break;
                default:
                    $scope.keyword = undefined;
                    break;
            }
        });

    });

})(angular);